#include<iostream>
const int N = 1e6 + 10;
int ne[N], e[N], mp[N], h, id, n,k,m,x,y;
using namespace std;
int main()
{
	id++;
	ne[h] = 1;
	mp[1] = id;
	e[id] = 1;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> m >> x;
		int p = mp[x];
		if (m == 1)
		{
			cin >> y;
			id++;
			e[id] = y;
			ne[id] = ne[p];
			ne[p] = id;
			mp[y] = id;
		}
		else if (m == 2)
		{
			cout << e[ne[p]] << endl;
		}
		else if (m == 3)
		{
			ne[p] = ne[ne[p]];
		}
	}

	return 0;
}